package controller;

import beans.LogEnum;
import dao.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import portal.mybatis.dao.User;
import portal.mybatis.dao.service.UserService;
import utils.UserLogUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Autowired
    private UserService userService;
    private static final long serialVersionUID = 1L;
    private RoleDao roleDao;

    public LoginServlet() {
        super();
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        //处理自动装配bean
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            doPost(request, response);
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
        String responseUrl = "";
        try {
            HttpSession session = request.getSession();

            String userName = request.getParameter("userName");
            String passWord = request.getParameter("passWord");
            String validate = request.getParameter("validate");

            if(userName.equals("")||passWord.equals("")){
                responseUrl = "-1";
            }
           /* else if (!session.getAttribute("IdentifyCode").equals(validate)) {
                // 验证码错误
                responseUrl = "2";
            }*/ else {
                User user = userService.checkUser(userName,passWord);
                if (user!=null) {
                    // 获取用户权限
                    int role_id = user.getUserRole();
                    roleDao = new RoleDao();
                    String power_code = (String) roleDao.showRoleDetail(role_id).get("powerCode");
                    // 将权限列表放入session对象
                    session.setAttribute("userName", user.getUserName());
                    session.setAttribute("userId", user.getUserId()+"");
                    session.setAttribute("userCode", power_code);
                    session.setAttribute("ctx", request.getContextPath());

                    Cookie userCode = new Cookie("userCode",power_code);
                    userCode.setMaxAge(24*3600);
                    response.addCookie(userCode);
                    Cookie userNameCookie = new Cookie("userName",user.getUserName());
                    userNameCookie.setMaxAge(24*3600);
                    response.addCookie(userNameCookie);
                    Cookie userIdCookie = new Cookie("userId",user.getUserId()+"");
                    userIdCookie.setMaxAge(24*3600);
                    response.addCookie(userIdCookie);
                    // 日志记录
                    UserLogUtil.dbLog(LogEnum.user,
                            LogEnum.login,
                            user.getUserId()+"",
                            LogEnum.user.getValue()+"编号："+user.getUserId()+"，名称："+user.getUserName());
                    responseUrl += "1,"+request.getContextPath() + "/security/security.do";
                }else{
                    // 用户输入信息或数据库连接有误，跳转到主页面
                    responseUrl = "0";
                }
//            } else if (!userDao.checkUser(userName, passWord)) {
//                // 用户输入信息有误，跳转到主页面
//                responseUrl = "0";
//            } else {
//                responseUrl = "-1";
//            }
            }
        }catch (NullPointerException e){
            //这里处理的是session过期后从session中取值出错
            responseUrl = "-1";
        }
        response.getWriter().write(responseUrl);
    }
}
